<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/base/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>${systemOption.systemTitle}</title>
    <%@ include file="/base/head.jsp"%>
    <script type="text/javascript" charset="UTF-8">
	//增加
  	function add_user(){
  		$('#deptid1').combotree({  
		           editable:false,
				   onClick:function(node) {
		       		  var b =  $(this).tree('isLeaf', node.target);
		      		  if(!b) {
		      			$('#deptid1').combotree('setValue','');
		      			return;
		      		 }
		           },
				   onShowPanel:function() {
		               $.ajax({
					        async: false,
					        type : "post",
					        lines: true,
					        url : '${ctx}/json/listDept.action',
					        success : function(data) {
					        	 $('#deptid1').combotree('loadData',data);
			       	
					        }
					    });
				   }
	   		});
  			$('#userAddWnd').show();
            $('#userAddWnd').dialog({
            	iconCls:'icon-user-add',
            	title:'增加用户',
            	modal:true,
            	buttons: [{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#userAddForm').form('submit',{  
					    success: function(data){   
					        if(data=='true'){  
					            $.messager.alert('友情提示','增加成功','info'); 
					            resetDG('#user'); 
					        }else{
					        	$.messager.alert('友情提示','增加失败','error'); 
					        } 
					        $('#userAddWnd').dialog('close');
					    }  
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#userAddWnd').dialog('close');
				}
		}]
            });                
  		$('#userAddForm').form('reset');
  		$('#firstBox').focus();  
  	}
  //修改
  function edit_user(){
  	$('#userAddForm').form('reset');
  	var row = $('#user').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择用户','error');
	}else{
		var userUpdateWnd;
		var userUpdateForm;
		var title;
		
		userUpdateWnd=$('#userUpdateWnd');
		userUpdateForm=$('#userUpdateForm');
		title="修改用户";
		
		$('#userDeptId').combotree({  
		           editable:false,
				   onClick:function(node) {
		       		  var b =  $(this).tree('isLeaf', node.target);
		      		  if(!b) {
		      			$('#userDeptId').combotree('setValue','');
		      			return;
		      		 }
		           },
				  /*  onShowPanel:function() {
		               $.ajax({
					        async: false,
					        type : "post",
					        lines: true,
					        url : '${ctx}/json/listDept.action',
					        success : function(data) {
					        	 $('#userDeptId').combotree('loadData',data);
			       				console.log(data);
					        }
					    });
				   } */
	   		});
		userUpdateWnd.show();
		userUpdateWnd.dialog({
        iconCls:'icon-user-edit',
        title:title,
        modal:true,
        buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					userUpdateForm.form('submit',{
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','修改成功','info');
					            resetDG('#user');
					        }else{
					        	$.messager.alert('友情提示','修改失败','error');
					        }
					        userUpdateWnd.dialog('close');
					    }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					userUpdateWnd.dialog('close');
				}
		}]
            });
            
				$.post("${ctx}/json/loadUser.action", {"id": row.id },
				   function(data){
				    if(data){
						$('#userId').val(data.id);
						$('#ssgsid').val(data.ssgsid);
						$('#userUserName').textbox('setValue',data.userName);
						$('#userSjhm').textbox('setValue',data.sjhm);
						$('#userSjnm').textbox('setValue',data.sjnm);
						$('#userName').textbox('setValue',data.name);
						$('#userDeptId').combotree('setValue',data.deptId);	
						$('#userStationId').combobox('setValue',data.stationId);			
					}else{
						$.messager.alert('友情提示','加载失败','error');
					}
				}, "json");
			

		}
	}
  //分页查询
  function doQuery(){

  	var queryParams=$('#user').datagrid('options').queryParams;
  	queryParams.userName=$('#userNameSch').val();
  	queryParams.name=$('#nameSch').val();
  	queryParams.status=$('#statusSch').combobox('getValue');
  	queryParams.deptId=$('#deptIdSch').combotree('getValue');
  	queryParams.stationId=$('#stationIdSch').combobox('getValue');
  	//重置当前页数为1
  	resetDG('#user');

  } 

	//根据不同值，显示不同内容
	function formatStatus(val,row){
		if (val == 1){
			return '<span style="color:#C0FF3E;">正常</span>';
		}else if (val == 2){
			return '<span style="color:red;">停用</span>';
		}else if (val == 3){
			return '<span style="color:blue;">离职</span>';
		}
		 else {
			return '无';
		}
	}

  //锁定用户
  function lock_user(){
  	var row = $('#user').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择用户','error');
	}else{
		
		if(row.status!=1){
			$.messager.alert('友情提示','只能锁定用户状态为正常的用户','error');
			return;
		}
		$.messager.confirm('友情提示','您确定锁定该用户吗？',function(r){   
			if (r){   
				$.post("${ctx}/json/lockUser.action", {"id": row.id },
					function(data){
						if(data){
							$.messager.alert('友情提示','锁定成功','info'); 
							resetDG('#user');
						}else{
							$.messager.alert('友情提示','锁定失败','error'); 
						}
					}, "json");   
			}   
		});
	}
  }	
  //解锁用户
  function unlock_user(){
  	var row = $('#user').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择用户','error');
	}else{
		if(row.status!=2){
			$.messager.alert('友情提示','只可选择锁定用户解锁','error');
		}else{
			$.messager.confirm('友情提示','您确定解锁该用户吗？',function(r){   
			if (r){   
				$.post("${ctx}/json/unlockUser.action", {"id": row.id },
					function(data){
						if(data){  
							$.messager.alert('友情提示','解锁成功！','info'); 
							resetDG('#user');
						}else{
							$.messager.alert('友情提示','解锁失败','error'); 
						}
					}, "json");   
			}   
		});	
		}
	}
  }
  //重置密码
  function resetpwd_user(){
  	var row = $('#user').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择用户','error');
	}else{
		$.messager.confirm('友情提示','您确定重置该用户的密码吗？',function(r){   
			if (r){   
				$.post("${ctx}/json/resetPwdUser.action", {"id": row.id },
					function(data){
						if(data){  
							$.messager.alert('友情提示','重置密码成功！密码:'+${systemOption.defaultPsw},'info'); 
							resetDG('#user');
						}else{
							$.messager.alert('友情提示','重置密码失败','error'); 
						}
					}, "json");   
			}   
		});	
	}
  }  
  
    </script>
  </head>
  
  <body>
    
<!-- 主窗口 -->  	 
    <table id="user" class="easyui-datagrid" data-options="fit:true,
    								rownumbers:true,
									singleSelect:true,
									striped:true,
									fitColumns:true,
									toolbar:'#tb',
									pagination:true,
									pageSize:20,
									pageList:[20,30,50,100],
									url:'${ctx}/json/listUser.action'">
		<thead>
			<tr>
				<th data-options="field:'ck',checkbox:true"></th>
				<th data-options="field:'name',align:'left',halign:'center'" width="150"><strong>姓名</strong></th>
				<th data-options="field:'userName',align:'center'" width="80"><strong>用户名</strong></th>
				<th data-options="field:'empId',align:'center',halign:'center'" width="100"><strong>工号</strong></th>
				<th data-options="field:'status',align:'center',formatter:formatStatus" width="50" ><strong>状态</strong></th>
				<th data-options="field:'deptName',align:'left',halign:'center'" width="150"><strong>部门</strong></th>
				<th data-options="field:'stationName',align:'left',halign:'center'" width="80"><strong>岗位</strong></th>
				<th data-options="field:'id',hidden:true"></th>
				<th data-options="field:'stationCode',hidden:true"></th>
			</tr>
		</thead>
		
	</table>
<!-- Datagrid工具栏 -->
<div id="tb">
	<table width="100%" cellpadding="0" cellspacing="0">
	<tr>
	   <td class="toolDiv">  		
	  		<a href="javascript:void(0)" onclick="add_user()" class="easyui-linkbutton" data-options="iconCls:'icon-user-add',plain:true">增加用户</a><span class="vline">|</span>
			<a href="javascript:void(0)" onclick="edit_user()" class="easyui-linkbutton" data-options="iconCls:'icon-user-edit',plain:true">修改用户</a><span class="vline">|</span>
			<a href="javascript:void(0)" onclick="lock_user()" class="easyui-linkbutton" data-options="iconCls:'icon-lock',plain:true">锁定用户</a><span class="vline">|</span>
			<a href="javascript:void(0)" onclick="unlock_user()" class="easyui-linkbutton" data-options="iconCls:'icon-lock-open',plain:true">解锁用户</a><span class="vline">|</span>
			<a href="javascript:void(0)" onclick="resetpwd_user()" class="easyui-linkbutton" data-options="iconCls:'icon-key',plain:true">密码重置</a>
		</td>
	</tr>
	<tr>
	   <td class="serchDiv">
	   		<table width="100%" cellpadding="0" cellspacing="0">
	   			<tr>
	   				<td>用户名</td>
	   				<td><input id="userNameSch" class="easyui-textbox" type="text" style="width: 120px;"></td>
	   				<td>部门</td>
	   				<td><input id="deptIdSch" class="easyui-combotree" value="" data-options="url:'${ctx}/json/listDept2.action'" style="width:155px"/></td>
	   				<td>状态</td>
	   				<td>
						<select id="statusSch" class="easyui-combobox" style="width:125px;">
							<option value="0">--全部--</option>
							<option value="1">正常</option>
							<option value="2">停用</option>
						</select>
	   				</td>
	   			</tr>
	   			<tr>
	   				<td>姓名</td>
	   				<td><input id="nameSch" class="easyui-textbox" type="text" style="width: 120px;"></td>
	   				<td>岗位</td>
	   				<td><input id="stationIdSch" class="easyui-combobox" data-options="
                        	                                                                url:'${ctx}/json/listStationTreeP.action',
                        	                                                                value:'0',
                        	                                                                valueField:'id',
                        	                                                                textField:'stationName',
                        	                                                                panelHeight:'200'" 
                        	                                                                style="width:155px" />
                    </td>
                    <td>
                    	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" 
								onclick="doQuery()">查询</a>
                    </td>
	   			</tr>
	   		</table>
		</td>
	</tr>
	</table>
</div>
<!--增加窗口-->
    <div id="userAddWnd"  style="width: 310px; height: 220px; padding: 5px; background: #fafafa;display: none">
    	<form id="userAddForm" method="post" action="${ctx }/json/saveUser.action">
                <table cellpadding=3>
                	<tr >
                        <td>用户名：</td>
                        <td><input id="firstBox" name="userName" class="easyui-textbox"  data-options="required:true" 
                        	validType="remote['${ctx}/json/checkUserName.action','userNameChk']" invalidMessage="人员代码重复!" style="width:200px"/></td>
                    </tr>
                    <tr>
                        <td>姓名：</td>
                        <td><input name="name" class="easyui-textbox"  data-options="required:true" style="width:200px"/></td>
                    </tr>
                    <tr>
                        <td>所属部门：</td>
                        <td>
                        	<input id="deptid1" name="deptId" class="easyui-combotree"  data-options="required:true" 
                        		style="width:200px"/>
	                    </td>
					</tr>
					<tr>
                        <td>所属岗位：</td>
                        <td>
							<input id="stationid1" class="easyui-combobox" name="stationId" data-options="
                        	                                                      url:'${ctx}/json/listStationTree.action',
                        	                                                      valueField:'id',
                        	                                                      textField:'stationName',
                        	                                                      editable:false,
                        	                                                      required:true,
                        	                                                      panelHeight:'200'" 
                        	                                                      style="width:200px" />	                    
                     	</td>
					</tr>
                </table>
        </form>
    </div>
<!--修改窗口-->
    <div id="userUpdateWnd"  style="width: 310px; height: 220px; padding: 5px; background: #fafafa;display: none">
    	<form id="userUpdateForm" method="post" action="${ctx }/json/saveUser.action">
    			<input id="userId" name="id" type="hidden"/>
    			<input id="ssgsid" name="ssgsid" type="hidden"/>
                <table cellpadding=3>
                	<tr>
                        <td>用户名：</td>
                        <td><input id="userUserName" name="userName" class="easyui-textbox"  readonly="readonly" style="width:200px"/></td>
                    </tr>
                    <tr>
                        <td>用户名：</td>
                        <td><input id="userUserName" name="empId" class="easyui-textbox"  readonly="readonly" style="width:200px"/></td>
                    </tr>
                    <tr>
                        <td>姓名：</td>
                        <td><input id="userName" name="name" class="easyui-textbox"  data-options="required:true" style="width:200px"/></td>
                    </tr>
                    <tr>
                        <td>所属部门：</td>
                        <td>
							<input id="userDeptId" name="deptId" class="easyui-combotree" data-options=" url:'${ctx}/json/listDept.action',
                        	                                                      valueField:'id',
                        	                                                      textField:'dname',
                        	                                                      editable:false,
                        	                                                      required:true,
                        	                                                      panelHeight:'200'" 
                        	                                                      style="width:200px"/>
						</td>
					</tr> 
					<tr>
                        <td>所属岗位：</td>
                        <td>
                        	
                        	<input id="userStationId" class="easyui-combobox" name="stationId" data-options="
                        	                                                      url:'${ctx}/json/listStationTree.action',
                        	                                                      valueField:'id',
                        	                                                      textField:'stationName',
                        	                                                      editable:false,
                        	                                                      required:true,
                        	                                                      panelHeight:'200'" 
                        	                                                      style="width:200px" />
	                    </td>
					</tr>
                </table>
        </form>
    </div>
    
  </body>
</html>
